package com.chucang.shucang.oms.executor;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/10/5 17:43
 * @description
 */
@AutoConfiguration
public class OmsExecutor {

    private static final int CORE_NUM = 8;

    private static final int MAX_NUM = 32;

    private static final int QUEUE_LENGTH = 256;

    private static final int KEEP_ALIVE = 600;

    @Bean
    public ThreadPoolTaskExecutor omsThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_NUM);
        executor.setMaxPoolSize(MAX_NUM);
        executor.setQueueCapacity(QUEUE_LENGTH);
        executor.setThreadNamePrefix("omsThreadPool_");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.setKeepAliveSeconds(KEEP_ALIVE);
        executor.initialize();
        return executor;
    }
}
